//
//  TTDianpuManagerHeaderView.m
//  TaoTuo
//
//  Created by admin on 2019/11/5.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuManagerHeaderView.h"
#import <UIImageView+WebCache.h>
#import "TTDianpuOrderManagerViewController.h"
#import "TTDianpuShareView.h"
#import <WXApi.h>
#import "TTPostersViewController.h"
#import <UIButton+WebCache.h>

@interface TTDianpuManagerHeaderView ()

@property (nonatomic, strong) UIButton *headerBtn;  // 头像
@property (nonatomic, strong) UILabel *titleLabel;
//@property (nonatomic, strong) UILabel *websiteLabel;
@property (nonatomic, strong) UIButton *websiteBtn; //网站名称
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, strong) UIView *orangeView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, strong) UILabel *bottomOneLabel;
@property (nonatomic, strong) UILabel *bottomTwoLabel;
@property (nonatomic, strong) UILabel *bottomThreeLabel;

@property (nonatomic, strong) TTDianpuShareView *shareView;

@end

@implementation TTDianpuManagerHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.subject = [RACSubject subject];
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubviews];
        
    }
    return self;
}

// 分享商城
- (void)shareDianpuBtnClick
{
    if (self.shareView == nil) {
        self.shareView = [TTDianpuShareView shareViewWithTitles:@[@"分享店铺海报", @"分享店铺到朋友圈", @"分享店铺给好友"] selectBlock:^(NSInteger index) {
            
            NSString *selStr = [NSString stringWithFormat:@"shareTo_%ld", index];
            SEL sel = NSSelectorFromString(selStr);
            if ([self respondsToSelector:sel]) {
                IMP imp = [self methodForSelector:sel];
                void (*func)(id, SEL) = (void *)imp;
                func(self, sel);
            }
        }];
    }
    
    [LLAnimationAlertView animationAlertViewWithView:self.shareView animationType:AnimationFromBottom];
}

///海报
- (void)shareTo_0
{
    TTPostersViewController *posterVC = [[TTPostersViewController alloc] init];
    [[LLRouteTool getCurrentNavigationController] pushViewController:posterVC animated:YES];
}

///朋友圈
- (void)shareTo_1
{
    [self shareToWeixinWithType:WXSceneTimeline];
}

///朋友
- (void)shareTo_2
{
    [self shareToWeixinWithType:WXSceneSession];
}

///店铺设置
- (void)gotoDianpuSet
{
    [self.subject sendNext:@1];
}

///查看自己店铺网站
- (void)gotoLookWebsite
{
    NSString *domainStr = [TTLoginSingle sharedSingle].userModel.ShopDomainName;
    if (domainStr.length > 0) {
        domainStr = [domainStr urlEncode];
    }
    NSURL *url = [NSURL URLWithString:domainStr];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
}

- (void)shareToWeixinWithType:(int)type
{
    if (![WXApi isWXAppInstalled]) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"未安装微信"];
        return;
    }
    WXWebpageObject *ext = [WXWebpageObject object];
    if (self.model.ShareUrl.length > 0) {
        ext.webpageUrl = self.model.ShareUrl;
    }
    
    // 压缩缩略图，大于64K不能分享
    UIImage *image = [UIImage imageWithData:self.imageData];
    UIImage *resultImage = [image zipImageWithSize:CGSizeMake(180, 180)];
    NSData *resultData = UIImageJPEGRepresentation(resultImage, 0.7);
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = self.model.ShareTitle;
    message.description = self.model.ShareDescription;
    [message setThumbData:resultData];
    message.mediaObject = ext;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = type;
    
    [WXApi sendReq:req completion:^(BOOL success) {
        NSString *str = success ? @"分享成功" : @"分享失败";
        [[UIApplication sharedApplication].keyWindow makeToast:str];
    }];
}

// 待下单
- (void)leftTapAction
{
    TTDianpuOrderManagerViewController *managerVC = [[TTDianpuOrderManagerViewController alloc] initWithDefaultIndex:0];
    [[LLRouteTool getCurrentNavigationController] pushViewController:managerVC animated:YES];
}

// 累计订单
- (void)rightTapAction
{
    TTDianpuOrderManagerViewController *managerVC = [[TTDianpuOrderManagerViewController alloc] initWithDefaultIndex:3];
    [[LLRouteTool getCurrentNavigationController] pushViewController:managerVC animated:YES];
}

- (void)setModel:(TTDianpuManagerModel *)model
{
    _model = model;
    NSString *urlString = [model.ShopLogo stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self.headerBtn sd_setImageWithURL:[NSURL URLWithString:urlString] forState:UIControlStateNormal];
    
    self.titleLabel.text = model.ShopName;
    NSString *websiteTitle = model.ShopDomainName2.length > 0 ? model.ShopDomainName2 : model.ShopDomainName;
    [self.websiteBtn setTitle:websiteTitle forState:UIControlStateNormal];
    
    NSString *pendString = [NSString stringWithFormat:@"%@单", model.PendOrder];
    NSString *leftString = [NSString stringWithFormat:@"%@\n待下单", pendString];
    NSMutableAttributedString *leftAttr = [self setAttributedString:leftString lineSpacing:6];
    [leftAttr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18], NSForegroundColorAttributeName : UIColorHex(FFBC22)} range:[leftString rangeOfString:pendString]];
    [leftAttr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:[pendString rangeOfString:@"单"]];
    self.leftLabel.attributedText = leftAttr;
    
    
    NSString *rightStr = [NSString stringWithFormat:@"%@\n累计订单", model.OrderTotalCount];
    NSMutableAttributedString *rightAttr = [self setAttributedString:rightStr lineSpacing:6];
    [rightAttr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18], NSForegroundColorAttributeName : UIColorHex(FFBC22)} range:[rightStr rangeOfString:model.OrderTotalCount]];
    self.rightLabel.attributedText = rightAttr;
    
    NSString *bottomOneString = [NSString stringWithFormat:@"今日浏览\n%@", model.ToDayBrowseNumber];
    NSMutableAttributedString *bottomOneAttr = [self setAttributedString:bottomOneString lineSpacing:3];
    [bottomOneAttr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11], NSForegroundColorAttributeName : UIColorHex(FFBC22)} range:[bottomOneString rangeOfString:model.ToDayBrowseNumber]];
    self.bottomOneLabel.attributedText = bottomOneAttr;
    
    
    NSString *bottomTwoString = [NSString stringWithFormat:@"访问人数\n%@", model.BrowseNumber];
    NSMutableAttributedString *bottomTwoAttr = [self setAttributedString:bottomTwoString lineSpacing:3];
    [bottomTwoAttr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11], NSForegroundColorAttributeName : UIColorHex(FFBC22)} range:[bottomTwoString rangeOfString:model.BrowseNumber]];
    self.bottomTwoLabel.attributedText = bottomTwoAttr;
    
    
    NSString *bottomThreeString = [NSString stringWithFormat:@"浏览次数\n%@", model.TotalBrowseNumber];
    NSMutableAttributedString *bottomThreeAttr = [self setAttributedString:bottomThreeString lineSpacing:3];
    [bottomThreeAttr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11], NSForegroundColorAttributeName : UIColorHex(FFBC22)} range:[bottomThreeString rangeOfString:model.TotalBrowseNumber]];
    self.bottomThreeLabel.attributedText = bottomThreeAttr;
    
    
}


- (void)setupSubviews
{
    _orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 126)];
    [_orangeView addGradientRampWithColors:@[UIColorHex(FFBB20), UIColorHex(FBCE65)] colorDirection:HorizontalDirection];
    [self addSubview:_orangeView];
    
    _headerBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@""] target:self action:@selector(gotoDianpuSet)];
    _headerBtn.backgroundColor = UIColorHex(f2f2f2);
    _headerBtn.layer.cornerRadius = 20;
    _headerBtn.layer.masksToBounds = YES;
    _headerBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_headerBtn];
    [_headerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(25);
        make.width.height.mas_equalTo(40);
    }];
    
    _titleLabel = [UILabel labelWithTitle:@"太平洋钻石珠宝店" color:[UIColor blackColor] fontSize:20];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerBtn);
        make.left.equalTo(self.headerBtn.mas_right).mas_offset(10);
    }];
    
    _websiteBtn = [UIButton createButtonWithTitle:@"www.sohuhdkdfl.com" titleColor:UIColorHex(5F4000) titleFont:[UIFont systemFontOfSize:11] backgroundColor:nil target:self action:@selector(gotoLookWebsite)];
    _websiteBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    [self addSubview:_websiteBtn];
    [_websiteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(2);
        make.left.equalTo(self.titleLabel);
    }];
    
    UIImage *image = [UIImage imageWithColor:UIColorHex(D6990C) rect:CGRectMake(0, 0, 85, 30) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(15, 15)];
    _shareBtn = [UIButton createButtonWithTitle:@"分享商城" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(shareDianpuBtnClick)];
    _shareBtn.layer.cornerRadius = 15;
    [_shareBtn setBackgroundImage:image forState:UIControlStateNormal];
    [self addSubview:_shareBtn];
    [_shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(27);
        make.right.equalTo(self);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(85);
    }];
    
    
    CGFloat space = 15;
    CGFloat leftLabelW = (self.width - 3 * space) * 0.5;
    CGFloat leftLabelH = 75;
    
    _leftLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
    _leftLabel.attributedText = [self setAttributedString:@"0单\n待下单" lineSpacing:6];
    _leftLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
    _leftLabel.numberOfLines = 0;
    _leftLabel.userInteractionEnabled = YES;
    [self addShadowForLabel:_leftLabel w:leftLabelW h:leftLabelH];
    [self addSubview:_leftLabel];
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerBtn.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(space);
        make.width.mas_equalTo(leftLabelW);
        make.height.mas_equalTo(leftLabelH);
    }];
    UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftTapAction)];
    [_leftLabel addGestureRecognizer:leftTap];
    
    _rightLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
    _rightLabel.attributedText = [self setAttributedString:@"108\n累计订单" lineSpacing:6];
    _rightLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
    _rightLabel.numberOfLines = 0;
    _rightLabel.userInteractionEnabled = YES;
    [self addShadowForLabel:_rightLabel w:leftLabelW h:leftLabelH];
    [self addSubview:_rightLabel];
    [_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(self.leftLabel);
        make.left.equalTo(self.leftLabel.mas_right).mas_offset(space);
    }];
    
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightTapAction)];
    [_rightLabel addGestureRecognizer:rightTap];
    
    CGFloat bottomLabelW = self.width / 3.0;
    _bottomOneLabel = [UILabel labelTextCenterWithTitle:@"" color:UIColorHex(333333) fontSize:12];
    _bottomOneLabel.attributedText = [self setAttributedString:@"今日浏览\n800" lineSpacing:3];
    _bottomOneLabel.numberOfLines = 0;
    [self addSubview:_bottomOneLabel];
    [_bottomOneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftLabel.mas_bottom).mas_offset(20);
        make.left.equalTo(self);
        make.width.mas_equalTo(bottomLabelW);
        make.height.mas_equalTo(40);
    }];
    
    _bottomTwoLabel = [UILabel labelTextCenterWithTitle:@"" color:UIColorHex(333333) fontSize:12];
    _bottomTwoLabel.attributedText = [self setAttributedString:@"访问人数\n1008" lineSpacing:3];
    _bottomTwoLabel.numberOfLines = 0;
    [self addSubview:_bottomTwoLabel];
    [_bottomTwoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(self.bottomOneLabel);
        make.left.equalTo(self.bottomOneLabel.mas_right);
    }];
    
    
    _bottomThreeLabel = [UILabel labelTextCenterWithTitle:@"" color:UIColorHex(333333) fontSize:12];
    _bottomThreeLabel.attributedText = [self setAttributedString:@"浏览次数\n308" lineSpacing:3];
    _bottomThreeLabel.numberOfLines = 0;
    [self addSubview:_bottomThreeLabel];
    [_bottomThreeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(self.bottomOneLabel);
        make.left.equalTo(self.bottomTwoLabel.mas_right);
    }];
    
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = UIColorHex(F1F5F6);
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(10);
    }];
    
    
}

- (void)addShadowForLabel:(UILabel *)lable w:(CGFloat)w h:(CGFloat)h
{
    lable.layer.cornerRadius = 5;
    lable.layer.shadowColor = UIColorHex(000000).CGColor;
    lable.layer.shadowOpacity = 0.15;
    lable.layer.shadowOffset = CGSizeZero;
    lable.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, w, h) cornerRadius:5].CGPath;
}

- (NSMutableAttributedString *)setAttributedString:(NSString *)string lineSpacing:(CGFloat)lineSpacing
{
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = lineSpacing;
    style.alignment = NSTextAlignmentCenter;    // 居中
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
    [attr addAttributes:@{NSParagraphStyleAttributeName : style} range:NSMakeRange(0, attr.string.length)];
    return attr;
}


@end
